Telegram Group & Telegram Channel
Embedded Academy
🔺Comparision of C++ and Posix Threads ✍️ B4b4k What is the difference between using the C++ std threads and POSIX threads? API: The API for C++ std threads and POSIX threads are different, with different function names and parameters. The C++ std thread…
One line down, more efficient: Tail Recursion

📌 B4b4k

Recursive functions are known for programmers, but it uses the call stack and has stack overflow risk. but simple change results in a big difference. this change is called "tail recursive". The tail recursion is that kind of recursion in which the recursive call is made at the end of the function.
Consider this formal recursion:

unsigned int fact(unsigned int n)
{
if (n <= 0)
return 1;
return n * fact(n - 1);
}

Can Change to the Tail-recursion version as follows:
unsigned int factTail(unsigned int n, unsigned int a)
{
if (n == 1)
return a;
return factTail(n - 1, n * a);
}
unsigned int fact(unsigned int n) { return factTail(n, 1); }

Note in this version there is no statement after the recursive call.
While computers execute recursive with the help of stacks By using tail recursive instead of formal or head recursive, compilers (such as GCC) can transform this to loop and eliminates stack overflow risk and decrease space complexity from O(n) to O(1).

#Tips #Algorithms #Cpp
@embedded



tg-me.com/embedded/2098
Create:
Last Update:

One line down, more efficient: Tail Recursion

📌 B4b4k

Recursive functions are known for programmers, but it uses the call stack and has stack overflow risk. but simple change results in a big difference. this change is called "tail recursive". The tail recursion is that kind of recursion in which the recursive call is made at the end of the function.
Consider this formal recursion:

unsigned int fact(unsigned int n)
{
if (n <= 0)
return 1;
return n * fact(n - 1);
}

Can Change to the Tail-recursion version as follows:
unsigned int factTail(unsigned int n, unsigned int a)
{
if (n == 1)
return a;
return factTail(n - 1, n * a);
}
unsigned int fact(unsigned int n) { return factTail(n, 1); }

Note in this version there is no statement after the recursive call.
While computers execute recursive with the help of stacks By using tail recursive instead of formal or head recursive, compilers (such as GCC) can transform this to loop and eliminates stack overflow risk and decrease space complexity from O(n) to O(1).

#Tips #Algorithms #Cpp
@embedded

BY Embedded Academy


Warning: Undefined variable $i in /var/www/tg-me/post.php on line 283

Share with your friend now:
tg-me.com/embedded/2098

View MORE
Open in Telegram


Embedded Academy Telegram | DID YOU KNOW?

Date: |

If riding a bucking bronco is your idea of fun, you’re going to love what the stock market has in store. Consider this past week’s ride a preview.The week’s action didn’t look like much, if you didn’t know better. The Dow Jones Industrial Average rose 213.12 points or 0.6%, while the S&P 500 advanced 0.5%, and the Nasdaq Composite ended little changed.

China’s stock markets are some of the largest in the world, with total market capitalization reaching RMB 79 trillion (US$12.2 trillion) in 2020. China’s stock markets are seen as a crucial tool for driving economic growth, in particular for financing the country’s rapidly growing high-tech sectors.Although traditionally closed off to overseas investors, China’s financial markets have gradually been loosening restrictions over the past couple of decades. At the same time, reforms have sought to make it easier for Chinese companies to list on onshore stock exchanges, and new programs have been launched in attempts to lure some of China’s most coveted overseas-listed companies back to the country.

Embedded Academy from us


Telegram Embedded Academy
FROM USA